﻿using CPF;
using CPF.Controls;
using CPF.Drawing;
using System;
using System.Collections.Generic;
using static System.Net.Mime.MediaTypeNames;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://UI/Style/css/NormalStyle.css")]//用于设计的时候加载样式
    public class OfficMenu : Control
    {
        OfficeHeader officeHeader;
        Panel tabMenu; 
        public List<OfficeMenuModel> OfficeMenuList = new List<OfficeMenuModel>();
        string SelectedTabName = "";
        //TabMenuHelper tabMenuHelper=new TabMenuHelper();
        //模板定义
        protected override void InitializeComponent()
        {
            officeHeader = new OfficeHeader()
            {
                MarginLeft = 70,
                MarginRight = 0,
                Height = 30,
                MarginTop = 0,
                ClipToBounds = true,
            };
            tabMenu = new Panel()
            {
                IsAntiAlias = true,
                MarginLeft = 1,
                MarginRight = 1,
                MarginBottom = 1,
                MarginTop = 29,
                Classes = "TabMenuContent",
                //Background = new LinearGradientFill()
                //{
                //    StartPoint = new PointField(0, 0),
                //    EndPoint = new PointField(0, "100%"),
                //    GradientStops =
                //    {
                //        new GradientStop(Color.FromArgb(255,231,238,247), 0),
                //        new GradientStop(Color.FromArgb(255,203,218,238), 0.2f),
                //        new GradientStop(Color.FromArgb(255,203,220,239), 0.3f),
                //        new GradientStop(Color.FromArgb(255,216,231,246), 0.7f),
                //        new GradientStop(Color.FromArgb(255,228,240,253), 1f)
                //    }
                //},
                //CornerRadius = "6",
                //BorderFill = Color.FromRgb(173, 191, 213),
                //BorderStroke = "1,Solid",
            };
            Children.Add(tabMenu);
            Children.Add(officeHeader);

        }
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            officeHeader.OnTabHeaderClick += TabHeader_OnTabHeaderClick;
        }

        private void TabHeader_OnTabHeaderClick(object sender, OfficeHeaderEvenArgs e)
        {
            OnTabHeadMenuClick(sender, e);
            CreatMenu(e.TabName);
        }

        //用户代码
        public void ClearTabHeader()
        {
            officeHeader.ClearTabHeader();
        }
        public Panel AddTabHeader(string name, bool isSelected)
        {
           return officeHeader.AddTabHeader(name, isSelected);
        }
 
        public StackPanel CreatMenu(string name)
        {
            SelectedTabName = name;
            //TabMenuList = tabMenuHelper.GetTabMenuList(name);
            tabMenu.Children.Clear();
            StackPanel menuHost = new StackPanel
            {
                MarginLeft = 3,
                MarginBottom = 3,
                MarginRight = 3,
                MarginTop = 3,
                Orientation = Orientation.Horizontal,
            };
            for (int i = 0; i < OfficeMenuList.Count; i++)
            {
                if (OfficeMenuList[i].IsWrap == false)
                {

                    StackPanel menuitemspnl = new StackPanel
                    {
                        Orientation = Orientation.Horizontal,
                        MarginLeft = 10,
                        MarginBottom = 20,
                        MarginRight = 10,

                        MarginTop = 0,
                    };
                    menuitemspnl.Tag = OfficeMenuList[i];
                    for (int j = 0; j < OfficeMenuList[i].Items.Count; j++)
                    {
                        menuitemspnl.Children.Add(AddMenuItem(OfficeMenuList[i].Items[j]));
                    }
                    Panel menuPanel = new Panel
                    {
                        ClipToBounds = true,
                        IsAntiAlias = true,
                        MarginRight = 3,
                        MarginTop = 0,
                        MarginBottom = 3,

                        Classes = "MenuContent",
                        Children =
                                {
                                    menuitemspnl,
                                    new TextBlock
                                    {
                                        Text=OfficeMenuList[i].Name,
                                        MarginBottom=0,
                                        MarginLeft=0,
                                        MarginRight=0,
                                        Height=20,
                                        TextAlignment=TextAlignment.Center,
                                        VerticalAlignment=VerticalAlignment.Center,
                                        Classes = "MenuBottonText",
                                    }
                                }
                    };
                    menuHost.Children.Add(menuPanel);
                }
                else
                {
                    WrapPanel menuitemspnl = new WrapPanel
                    {
                        Orientation = Orientation.Horizontal,
                        Width = OfficeMenuList[i].Width,

                    };
                    menuitemspnl.Tag = OfficeMenuList[i];
                    for (int j = 0; j < OfficeMenuList[i].Items.Count; j++)
                    {
                        menuitemspnl.Children.Add(AddMenuItem(OfficeMenuList[i].Items[j]));
                    }
                    ScrollViewer sv = new ScrollViewer
                    {
                        MarginTop = 0,
                        MarginBottom = 20,
                        Width = OfficeMenuList[i].Width,
                        Content = menuitemspnl,
                        HorizontalScrollBarVisibility=ScrollBarVisibility.Hidden
                        
                    };
                    Panel menuPanel = new Panel
                    {
                        ClipToBounds = true,
                        IsAntiAlias = true,
                        MarginTop=0,
                        MarginBottom=3,
                        MarginRight = 3,

                        Classes = "MenuContent",
                        Children =
                                {
                                    sv,
                                    new TextBlock
                                    {
                                        Text=OfficeMenuList[i].Name,
                                        MarginBottom=0,
                                        MarginLeft=0,
                                        MarginRight=0,
                                        Height=20,
                                        TextAlignment=TextAlignment.Center,
                                        VerticalAlignment=VerticalAlignment.Center,
                                        Classes = "MenuBottonText",
                                    }
                                }
                    };
                    menuHost.Children.Add(menuPanel);
                }
            }

            tabMenu.Children.Add(menuHost);
            return menuHost;
        }
        public Panel AddMenuItem(OfficeMenuItem menuitem)
        {
            Panel menu;
            switch (menuitem.MenuType)
            {
                case OfficeMenuType.None:
                    menu = new Panel { };
                    return menu;
                case OfficeMenuType.Split:
                    menu = new Panel
                    {
                        MarginTop = 0,
                        MarginBottom = 0,
                        Width = 3,
                        MarginRight = 10,
                        Children ={
                            new Border{
                                    Width=2,
                                    MarginBottom=5,
                                    Classes = "MenuSplitItem",
                                    BorderFill=null,
                                    MarginTop=5,
                            },
                        }
                    };
                    return menu;
                case OfficeMenuType.Button:
                    menu = new Panel
                    {
                        Cursor = Cursors.Hand,
                        IsAntiAlias = true,
                        MarginTop = 3,
                        MarginBottom = 3,
                        MinWidth = 80,
                        MarginRight = 5,
                        MarginLeft = 5,
                        CornerRadius = "4",
                        Classes = "MenuItem",
                        Tag= menuitem,
                        Children =
                        {
                            new Picture{
                                Width=30,
                                Height=30,
                                MarginTop=5,
                                Stretch = Stretch.Fill,
                                Source = menuitem.Image,
                            },
                            new TextBlock
                            {
                                Classes = "MenuItemText",
                                Name="menuItemText",
                                Text=menuitem.Name,
                                MarginTop=40,
                            }
                        }
                    };
                    menu.MouseUp += Menu_MouseUp;
                    return menu;
                case OfficeMenuType.ListButton:
                    menu = new Panel
                    {
                        Cursor = Cursors.Hand,
                        IsAntiAlias = true,
                        MarginTop = 3,
                        MarginBottom = 3,
                        MarginRight = 3,
                        MarginLeft = 3,
                        CornerRadius = "4",
                        Classes = "MenuItem",
                        Tag = menuitem,
                        Children =
                        {
                            new Picture{
                                Width=30,
                                Height=30,
                                Stretch = Stretch.Fill,
                                Source = menuitem.Image,
                                ToolTip=menuitem.Name
                            }
                        }
                    };
                    menu.MouseUp += Menu_MouseUp;
                    return menu;
            }
            return null;

        }

        private void Menu_MouseUp(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            Panel menu = ((Panel)sender);
            if (menu.IsMouseOver)
            {
                Panel menuhost = menu.Parent as Panel;
                OfficeMenuItem tabMenuItem = menu.Tag as OfficeMenuItem;
               OfficeMenuModel tabMenuModel= menuhost==null?null: menuhost.Tag as OfficeMenuModel;
                TabMenuClick(sender, new OfficeMenuEvenArgs(SelectedTabName, tabMenuModel, tabMenuItem, e));
            }
        }


        public event EventHandler<OfficeHeaderEvenArgs> OnTabHeadMenuClick;
        public void TabHeadMenuClick(object sender, OfficeHeaderEvenArgs e)
        {
            OnTabHeadMenuClick?.Invoke(sender, e);
        }
        public void TabMenuClick(object sender, OfficeMenuEvenArgs e)
        {
            OnTabMenuClick?.Invoke(sender, e);
        }
        public event EventHandler<OfficeMenuEvenArgs> OnTabMenuClick;
#endif
    }
}
